<?php 

require(ROOT_PATH.'/include/Action.php');

class Action extends mainAction
{
	
	public $adminid		= 0;
	public $adminname	= '';
	public $adminrs;
	public $admintype	= 0;
	public $loginback	= '';
	public $maxwidth 	= '80%';
	public $deflogin 	= '?m=login';//默认登录页地址
	
	
	//单位相关
	public $cnum = '';
	public $companyid = 0;
	public $companyinfo;
	
	//用户
	public $useraid 	= 0;
	public $userainfo 	= array();
	
	
	public function loginCheck()
	{
		$loginbo  = false;
		if($this->adminid>0){
			$dlur = m('login')->getone("`uid`='$this->adminid'");
			if($dlur){
				$loginbo = true;
			}
		}
		if(!$loginbo){
			if($this->adminid>0)m('login')->exitlogin();
			$url = $this->deflogin;
			if($this->loginback)$url.='&backurl='.$this->jm->base64encode($this->loginback).'';
			if(isajax())$this->showreturn('','登录失效了', 199);
			$this->rock->location($url);
			return;
		}
		$this->getAdmininfo($this->adminid);
	}
	
	public function initProject()
	{
		$this->assign('maxwidth', getconfig('maxwidth', $this->maxwidth));
		$this->adminid		= (int)$this->getsession('adminid',0);
		$this->loginCheck();
	}
	
	public function getAdmininfo($uid)
	{
		$this->adminid  = $uid;
		$this->adminrs	= m('users')->getone("`id`='$uid' and `status`=1");
		if(!$this->adminrs)$this->showreturn('','无效登录了', 199);
		$this->adminrs['face'] = m('users')->getface($this->adminrs['face']);
		$this->admintype= $this->adminrs['type'];
		$this->adminname= $this->adminrs['name'];
		$this->rock->adminid 	= $this->adminid;
		$this->rock->adminname 	= $this->adminname;
		$this->assign('adminrs', $this->adminrs);
		$this->assign('cnum', '');
	}
	
	public function returnerror($msg='')
	{
		return returnerror($msg);
	}
	
	public function returndata($data=array())
	{
		return returnsuccess($data);
	}
	
	
	
	public function savecellAction()
	{
		$tab 	= (int)$this->post('tablename','0');
		$id 	= (int)$this->post('id','0');
		$fields = $this->post('fieldname');
		$value 	= $this->post('value');
		$tabla	= array('agent','users','wenti','party','mess','file','dept','usera','company','chatgroup');
		$table  = arrvalue($tabla, $tab);
		if(isempt($table) || $id<=0)return returnsuccess('没有找到表');
		m($table)->update(array($fields => $value), $id);	
		return returnsuccess();
	}
	
	public function deljiluAction()
	{
		$tab 	= (int)$this->post('tablename','0');
		$id 	= (int)$this->post('id','0');
		$tabla	= array('agent','users','wenti','party','mess','file','dept','usera','company','chatgroup','log');
		$table  = arrvalue($tabla, $tab);
		if(isempt($table) || $id<=0)return returnerror('没有找到表');
		$dbs 	= m($table);
		if(method_exists($dbs,'deletebefore')){
			$str = $dbs->deletebefore($id);
			if(!isempt($str))return returnerror($str);
		}
		if($tab==5){
			$rs 	= $dbs->getone($id);
			if($rs){
				$filepath  = $rs['filepath'];
				$thumbpath = $rs['thumbpath'];
				if(!isempt($filepath) && file_exists($filepath))@unlink($filepath);
				if(!isempt($thumbpath) && file_exists($thumbpath))@unlink($thumbpath);
			}
		}
		$dbs->delete($id);	
		return returnsuccess();
	}
	
	public function showPage($msg)
	{
		if(isajax()){
			echo json_encode(returnerror($msg));
		}else{
			echo $msg;
		}
		exit();
	}
	
	public function showCompany($bh='',$bo=false)
	{
		$cnum 	= $this->post('cnum', $bh);
		if(isempt($cnum))$this->showPage('无效cnum1');
		$where 	= "`num`='$cnum'";
		if(is_numeric($bh))$where='`id`='.$bh.'';
		$dbs 	= m('company');
		$info 	= $dbs->getone($where);
		if(!$info)$this->showPage('无效单位');
		$this->title 	= $info['name'].'_单位管理';
		$this->cnum 	= $info['num'];
		$info['logo']	= getface($info['logo'],$dbs->deflogo);
		$this->companyinfo = $info;
		$this->companyid   = $info['id'];
		if($this->adminid>0){
			$ars = m('usera')->getone('`cid`='.$this->companyid.' and `uid`='.$this->adminid.'');
			if($ars){
				$this->userainfo = $ars;
				$this->useraid 	 = $ars['id'];
			}
		}
		if($bo){
			if($info['uid']!=$this->adminid){
				if(arrvalue($ars,'type')!='1')$this->showPage('不是单位管理员，返回<a href="?m=index">个人主页</a>');
			}
		}
		$this->assign('companyinfo', $info);
		$this->assign('cnum', $this->cnum);
		$this->assign('userainfo', $this->userainfo);
	}
}

class ActionNot extends Action
{
	public function loginCheck()
	{
		
	}
}

class ActionPlat extends Action
{
	public function loginCheck()
	{
		parent::loginCheck();
		$this->title = TITLE.'_平台管理';
		if($this->adminrs['type']!='1')exit('非后台管理员无法进入，<a href="javascript:;" onclick="history.back()">返回</a>');
	}
}

class ActionManage extends Action
{

	public function loginCheck()
	{
		parent::loginCheck();
		$this->showCompany('', true);
	}
}